package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.core.model.User;
import cn.edu.tju.core.security.SecurityUtils;
import cn.edu.tju.core.security.service.UserService;
import cn.edu.tju.elm.model.Remark;
import cn.edu.tju.elm.service.RemarkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/remarks")
@Tag(name = "评价管理", description = "评价的相关接口")
public class RemarkController {

    @Autowired
    private RemarkService remarkService;

    @Autowired
    private UserService userService;

    /**
     * 根据商家ID获取所有评价
     * @param businessId 商家ID
     * @return 评价列表
     */
    @GetMapping("/business/{businessId}")
    @Operation(summary = "根据商家ID获取评价", description = "获取指定商家的所有评价")
    public ResponseEntity<HttpResult<List<Remark>>> listRemarksByBusinessId(@PathVariable Long businessId) {
        try {
            List<Remark> remarks = remarkService.listRemarksByBusinessId(businessId);
            return ResponseEntity.ok(HttpResult.success(remarks));
        } catch (Exception e) {
            return ResponseEntity.ok(HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage()));
        }
    }

    /**
     * 保存评价
     * @param remark 评价对象
     * @return 保存结果
     */
    @PostMapping("")
    @Operation(summary = "保存评价", description = "用户对商家进行评价")
    public ResponseEntity<HttpResult<Remark>> saveRemark(@RequestBody Remark remark) {
        try {
            remark.setUser(remark.getUser());
            Remark savedRemark = remarkService.saveRemark(remark);
            return ResponseEntity.ok(HttpResult.success(savedRemark));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.ok(HttpResult.failure(ResultCodeEnum.BAD_REQUEST, e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.ok(HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage()));
        }
    }

    /**
     * 删除用户的所有评价
     * @param userId 用户ID
     * @return 删除结果
     */
    @DeleteMapping("/user/{userId}/all")
    @Operation(summary = "删除用户所有评价", description = "删除指定用户的所有评价")
    public ResponseEntity<HttpResult<List<Remark>>> removeAllRemarksByUserId(@PathVariable Long userId) {
        try {
            List<Remark> remarks = remarkService.removeAllRemarksByUserId(userId);
            return ResponseEntity.ok(HttpResult.success(remarks));
        } catch (Exception e) {
            return ResponseEntity.ok(HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage()));
        }
    }

    /**
     * 删除用户指定的评价
     * @param userId 用户ID
     * @param remarkId 评价ID
     * @return 删除结果
     */
    @DeleteMapping("/user/{userId}/remark/{remarkId}")
    @Operation(summary = "删除指定评价", description = "删除用户指定的评价")
    public ResponseEntity<HttpResult<List<Remark>>> removeOneRemarkByUserId(
            @PathVariable Long userId, 
            @PathVariable Long remarkId) {
        try {
            List<Remark> remarks = remarkService.removeOneRemarkByUserId(userId, remarkId);
            return ResponseEntity.ok(HttpResult.success(remarks));
        } catch (Exception e) {
            return ResponseEntity.ok(HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage()));
        }
    }

    /**
     * 根据用户ID获取所有评价
     * @param userId 用户ID
     * @return 评价列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "根据用户ID获取评价", description = "获取指定用户的所有评价")
    public ResponseEntity<HttpResult<List<Remark>>> getRemarksByUserId(@PathVariable Long userId) {
        try {
            List<Remark> remarks = remarkService.getRemarksByUserId(userId);
            return ResponseEntity.ok(HttpResult.success(remarks));
        } catch (Exception e) {
            return ResponseEntity.ok(HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage()));
        }
    }

    /**
     * 根据用户ID和商家ID获取评价
     * @param userId 用户ID
     * @param businessId 商家ID
     * @return 评价列表
     */
    @GetMapping("/user/{userId}/business/{businessId}")
    @Operation(summary = "根据用户和商家获取评价", description = "获取指定用户对指定商家的评价")
    public ResponseEntity<HttpResult<List<Remark>>> getRemarksByUserIdAndBusinessId(
            @PathVariable Long userId, 
            @PathVariable Long businessId) {
        try {
            List<Remark> remarks = remarkService.getRemarksByUserIdAndBusinessId(userId, businessId);
            return ResponseEntity.ok(HttpResult.success(remarks));
        } catch (Exception e) {
            return ResponseEntity.ok(HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage()));
        }
    }
}
